package com.ai.flight.ai.tools;

import com.ai.flight.business.record.FlightBooking;
import com.ai.flight.business.service.FlightService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.stereotype.Component;

/**
 * @Author: suren@517na.com
 * @CreateTime: 2025-06-02
 * @Description:  AI调用工具方法
 */

@Component
public class FlightBookingAiTools {

    private static final Logger logger = LoggerFactory.getLogger(FlightBookingAiTools.class);

    @Autowired
    private FlightService flightService;

    @Tool(description = "查询预定详情信息")
    public FlightBooking.BookingDetails getBookingDetails(@ToolParam(description = "预定号") String bookingNumber, @ToolParam(description = "姓名") String name) {
        try {
            return flightService.getBookingDetails(bookingNumber, name);
        } catch (Exception e) {
            logger.warn("Booking details: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
            return new FlightBooking.BookingDetails(bookingNumber, name, null, null, null, null, null);
        }
    }


    @Tool(description = "修改机票预定日期")
    public void changeBooking(@ToolParam(description = "预定号") String bookingNumber,
                              @ToolParam(description = "姓名") String name,
                              @ToolParam(required = false, description = "新的预定日期") String newDate,
                              @ToolParam(required = false, description = "出发地") String from,
                              @ToolParam(required = false, description = "目的地") String to) {
        flightService.changeBooking(bookingNumber, name, newDate, from, to);
    }

    @Tool(description = "取消机票预定")
    public void cancelBooking(@ToolParam(description = "预定号") String bookingNumber, @ToolParam(description = "姓名") String name) {
        flightService.cancelBooking(bookingNumber, name);
    }

}
